package com.addict.applocker.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.ColorInt;
import android.util.Log;

import com.addict.applocker.R;

import static com.addict.applocker.manager.SettingManager.Locker_Site.Right;

/**
 * 软件设置数据
 */
public class SettingManager {
    private static String SET_NAME = "Setting";

    public enum Locker_Site {Left, Right}
    private int mThemeColor;               //主题颜色
    private boolean mOpenLocker;          //是否开启桌面抽屉
    private Locker_Site mLockerSite;       //桌面抽屉位置
    private int mLockerWidth;             //抽屉隐藏时显露出来的宽度
    private boolean mScreenLockOff;      //锁屏时关闭所有程序
    private boolean mShutDownOff;        //关机时关闭所有程序
    private Context mContext = null;


    //单例
    private SettingManager() {
        mThemeColor = 0x1FBE9E;
        mOpenLocker = true;
        mLockerSite = Locker_Site.Right;
        mLockerWidth = 5;
        mScreenLockOff = false;
        mShutDownOff = false;
    }
    private static SettingManager me = null;

    public static SettingManager getInstance() {
        if(me == null)
            throw  new NullPointerException("SettingManager没有初始化，请先调用init()函数。");
        return me;
    }

    /**
     *   初始化并构建单例类。为了让那些不是Context的子类也能方便的
     * 访问这个类，所以没有放到Application类里面。
     */
    public static void init(Context context) {
        if(me != null) { me.mContext = context; return; }
        me = new SettingManager();
        me.mContext = context;
        me.read();
    }

    /**
     * 开线程读取设置，以免卡UI线程
     */
    public void read() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                readSetting();
            }
        }).start();
    }

    /**
     * 开线程写入设置，以免卡UI线程
     */
    public void write() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                writeSetting();
            }
        }).start();
    }

    //具体实现
    private synchronized void readSetting() {
        synchronized (this) {
            checkContextIsNull();
            SharedPreferences sp = mContext.getSharedPreferences(SET_NAME, Context.MODE_PRIVATE);
            if(sp == null) {
                Log.e("Debug:" + this.toString(), "in read:SharedPreferences获取失败！");
                return ;
            }
            mThemeColor = sp.getInt("ThemeColor", mContext.getResources().getColor(R.color.color_theme_13));
            mOpenLocker = sp.getBoolean("OpenLocker", false);
            mLockerSite = sp.getBoolean("LockerSite", false) ? Locker_Site.Left : Locker_Site.Right; // 就两个，好想用bool...
            mLockerWidth = sp.getInt("LockerWidth", 3); //默认3px
            mScreenLockOff = sp.getBoolean("ScreenLockoff", false);
            mShutDownOff = sp.getBoolean("ShutDownOff", false);
        }
    }
    private synchronized void writeSetting() {
        synchronized (this) {
            checkContextIsNull();
            SharedPreferences sp = mContext.getSharedPreferences(SET_NAME, Context.MODE_PRIVATE);
            if(sp == null) {
                Log.e("Debug:" + this.toString(), "in write:SharedPreferences获取失败！");
                return ;
            }
            SharedPreferences.Editor editor = sp.edit();
            if(editor == null) {
                Log.e("Debug:" + this.toString(), "in write:SharedPreferences.Editor获取失败！");
                return ;
            }
            editor.putInt("ThemeColor", mThemeColor);
            editor.putBoolean("OpenLocker", mOpenLocker);
            editor.putBoolean("LockerSite", mLockerSite == Locker_Site.Left);
            editor.putInt("LockerWidth", mLockerWidth);
            editor.putBoolean("ScreenLockoff", mScreenLockOff);
            editor.putBoolean("ShutDownOff", mShutDownOff);
            editor.commit();
        }
    }

    // 判断Context是否为空
    private void checkContextIsNull() {
        if(mContext == null)
            throw new NullPointerException("SettingManager没有初始化，请先调用init()函数。");
    }

    // get and set
    public int getThemeColor() {
        return mThemeColor;
    }

    public void setThemeColor(@ColorInt int mThemeColor) {
        this.mThemeColor = mThemeColor;
    }

    public boolean isOpenLocker() {
        return mOpenLocker;
    }

    public void setOpenLocker(boolean mOpenLocker) {
        this.mOpenLocker = mOpenLocker;
    }

    public Locker_Site getLockerSite() {
        return mLockerSite;
    }

    public void setLockerSite(Locker_Site mLockerSite) {
        this.mLockerSite = mLockerSite;
    }

    public int getLockerWidth() {
        return mLockerWidth;
    }

    public void setLockerWidth(int mLockerWidth) {
        this.mLockerWidth = mLockerWidth;
    }

    public boolean isScreenLockOff() {
        return mScreenLockOff;
    }

    public void setScreenLockOff(boolean mScreenLockOff) {
        this.mScreenLockOff = mScreenLockOff;
    }

    public boolean isShutDownOff() {
        return mShutDownOff;
    }

    public void setShutDownOff(boolean mShutDownOff) {
        this.mShutDownOff = mShutDownOff;
    }
}
